﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using DG.Tweening;

public class Level20MoveLetters : MonoBehaviour
{
    public Image bgImage;
    public GameLevelItem gameLevelItem;
    public List<Image> nodeList = new List<Image>();
    public List<TextButton> tbList = new List<TextButton>();
    private List<string> letterList = new List<string>();
    private Dictionary<int, TextButton> tbDic = new Dictionary<int, TextButton>();
    private Dictionary<int, int> tbIndexDic = new Dictionary<int, int>();   // key 是 tb index value 是 tb所在node 的index
    private int emptyNodeIndex = 19;
    private List<int> canMoveIndexList = new List<int>();
    // Start is called before the first frame update
    void Start()
    {
        InitLetterList();
        InitTbList();
        GetCanMoveIndexList();
    }

    private void InitLetterList()
    {
        letterList.Add("l");
        letterList.Add("a");
        letterList.Add("b");
        letterList.Add("f");
        letterList.Add("d");
        letterList.Add("k");
        letterList.Add("g");
        letterList.Add("i");
        letterList.Add("h");
        letterList.Add("c");
        letterList.Add("p");
        letterList.Add("e");
        letterList.Add("q");
        letterList.Add("j");
        letterList.Add("o");
        letterList.Add("m");
        letterList.Add("n");
        letterList.Add("v");
        letterList.Add("r");
    }

    private void InitTbList()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            tbList[tempIndex].Init(letterList[tempIndex], () =>
            {
                TBButtonClick(tempIndex);
            });
            tbList[tempIndex].transform.parent = nodeList[tempIndex].transform;
            tbList[tempIndex].transform.localPosition = Vector3.zero;
            tbDic.Add(tempIndex, tbList[tempIndex]);
            tbIndexDic.Add(tempIndex, tempIndex);    // 开始的时 key 和value 是相同的
        }
        tbDic.Add(emptyNodeIndex, null);
    }

    private void TBButtonClick(int index)
    {
        if (canMoveIndexList.Contains(tbIndexDic[index]))
        {
            EnableTBButton(false);
            tbList[index].transform.parent = nodeList[emptyNodeIndex].transform;
            tbList[index].transform.DOLocalMove(Vector3.zero, 0.2f).OnComplete(() =>
            {
                int curEmptyNodeIndex = emptyNodeIndex;
                tbDic[emptyNodeIndex] = tbList[index];
                emptyNodeIndex = tbIndexDic[index];
                tbIndexDic[index] = curEmptyNodeIndex;
                tbDic[emptyNodeIndex] = null;

                GetCanMoveIndexList();
                EnableTBButton(true);
                JudgeResult();
            });
        }
    }

    private void EnableTBButton(bool enable)
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            tbList[i].button.enabled = enable;
        }
    }

    private void GetCanMoveIndexList()
    {
        canMoveIndexList.Clear();

        int tempIndex = emptyNodeIndex - 1;     // 左
        if (tempIndex >= 0 && tempIndex / 4 == emptyNodeIndex / 4)
            canMoveIndexList.Add(tempIndex);

        tempIndex = emptyNodeIndex + 1;     // 右
        if (tempIndex < nodeList.Count && tempIndex / 4 == emptyNodeIndex / 4)
            canMoveIndexList.Add(tempIndex);

        tempIndex = emptyNodeIndex - 4;     // 上
        if (tempIndex >= 0)
            canMoveIndexList.Add(tempIndex);

        tempIndex = emptyNodeIndex + 4;     // 下
        if (tempIndex < nodeList.Count)
            canMoveIndexList.Add(tempIndex);

        Debug.Log("  -------can move index count " + canMoveIndexList.Count);
        for (int i = 0; i < canMoveIndexList.Count; i++)
        {
            Debug.Log("can move node :" + canMoveIndexList[i]);
        }

    }

    private void JudgeResult()
    {
        string resStr = string.Empty;
        for (int i = 0; i < 5; i++)
        {
            resStr = string.Empty;
            for (int j = 0; j < 4; j++)
            {
                int index = i * 4 + j;
                TextButton tb = null;
                tbDic.TryGetValue(index, out tb);
                if (null != tb)
                {
                    resStr += tb.text.text;
                }
            }
            Debug.Log("res str :" + resStr);
            if (resStr.Equals(UConst.Str_TargetLove))
            {
                Invoke("WinAni", 0.5f);
                EnableTBButton(false);
                break;
            }
        }
    }

    private void WinAni()
    {
        int count = tbList.Count;
        for (int i = 0; i < count; i++)
        {
            if (!UConst.Str_TargetLove.Contains(tbList[i].text.text))
            {
                tbList[i].button.image.DOFade(0, 0.5f);
                tbList[i].text.DOFade(0, 0.5f);
            }
        }

        bgImage.DOFade(0, 0.5f);

        Invoke("TriggerResult", 0.8f);
    }

    private void TriggerResult()
    {
        gameLevelItem.TriggerResult(true);
    }
}
